/*
 * Created on May 7, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.pyant.tasks;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.FileSet;


/**
 * @author Ron Smith
 *
 * <py-doc pythonpathref="project.python.path" defaultexcludes="true"
 *   destdir="docs">
 *   <fileset dir="testscripts2">
 *     <include name="** /*"/>
 * 	 </fileset>
 * </py-doc>
 */
public class PythonDocTask extends PythonInteractiveBaseTask
{
	protected ArrayList filesets = new ArrayList();
	protected boolean defaultExcludes = true;
	protected File destdir = null;
	
	private final static String FAIL_MSG = "Documentation generation failed; See output for details";
	
	public void execute()
	{
		Project project = getProject();
		project.log("PythonDocTask.execute()", Project.MSG_DEBUG);
		project.log("destdir=" + destdir);
		
		// TODO: Fail if destdir not set

		String modulesArg = getModulesArg(project);

		String script = "import pydoc;import sys;sys.argv=['aa','-w', " + modulesArg + "];pydoc.cli()";

		project.log("modules: " + modulesArg);

		this.script = script;
		
		executeScript(project, this.destdir, FAIL_MSG);

		
	}

	public void setDestdir(File destdir)
	{
		this.destdir = destdir;
	}
	
	/**
	 * 
	 * @param project
	 */
	protected String getModulesArg(Project project) {
		StringBuffer modulesArg = new StringBuffer();
		boolean isFirst = true;
		Iterator fsIter = filesets.iterator();
		while(fsIter.hasNext())
		{
			FileSet fs = (FileSet)fsIter.next();
			DirectoryScanner ds = fs.getDirectoryScanner(project);
			String[] files = ds.getIncludedFiles();
			for(int i = 0; i < files.length; i++)
			{
				String fileName = files[i];
				
				if(defaultExcludes)
				{
					// Exclude .pyc and .pyo files
					if(fileName.endsWith(".pyc") || fileName.endsWith(".pyo"))
					{
						continue;
					}
				}
				if(fileName.endsWith(".py"))
				{
					fileName = fileName.substring(0, fileName.length() - 3);
				}
				
				if(isFirst)
				{
					modulesArg.append("'" + fileName + "'");
					isFirst = false;
				}
				else
				{
					modulesArg.append(",'" + fileName + "'");
				}
			}
			
			String[] dirs = ds.getIncludedDirectories();
			for(int i = 0; i < dirs.length; i++)
			{
				String dirName = dirs[i];
				if(isFirst)
				{
					modulesArg.append("'" + dirName + "'");
					isFirst = false;
				}
				modulesArg.append(",'" + dirName + "'");				
			}
		}

		String retVal = modulesArg.toString();
		retVal = retVal.replace('\\', '.');
		retVal = retVal.replace('/', '.');
		return (retVal);
	}

	public void addFileSet(FileSet fs)
	{
		this.filesets.add(fs);
	}

	/**
	 * @return Returns the defaultExcludes.
	 */
	public boolean isDefaultExcludes() {
		return defaultExcludes;
	}
	/**
	 * @param defaultExcludes The defaultExcludes to set.
	 */
	public void setDefaultExcludes(boolean defaultExcludes) {
		this.defaultExcludes = defaultExcludes;
	}

}
